﻿/* 
This file is part of Ingo Karstein's Excel Export project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://spexcelexport.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    Ingo Karstein's SharePoint 2010 Custom Ribbon Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.ApplicationPages.Calendar;
using Microsoft.SharePoint.Diagnostics;
using Microsoft.SharePoint.Utilities;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Web;
using System.Web.Script.Serialization;
using System.Diagnostics;
using ik.SharePoint2010.ExcelExport.Code;

namespace ik.SharePoint2010.ExcelExport.Layouts
{
    [Guid("313cdab4-badd-443f-ba8c-504c2ceab191")]
    public class ExcelExportService : IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            string listname = "";
            string view = "";
            string url = "";
            string listParams = "";
            string listQuery = "";
            foreach( string k in context.Request.QueryString.AllKeys )
            {
                if( k.EndsWith("_ListName", StringComparison.InvariantCultureIgnoreCase) )
                {
                    listname = context.Request[k];
                }
                else if( k.EndsWith("_View", StringComparison.InvariantCultureIgnoreCase) )
                {
                    view = context.Request[k];
                }
                else if( k.EndsWith("_url", StringComparison.InvariantCultureIgnoreCase) )
                {
                    url = context.Request[k];
                }
                else if( k.EndsWith("_params", StringComparison.InvariantCultureIgnoreCase) )
                {
                    listParams = HttpUtility.HtmlDecode(context.Request[k]);
                }
                else if( k.EndsWith("_query", StringComparison.InvariantCultureIgnoreCase) )
                {
                    listQuery = HttpUtility.HtmlDecode(context.Request[k]);
                };
            };

            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists[new Guid(listname)];

            string exp = ExportToXml.Export(list, new Guid(view), url, listParams, listQuery);
            byte[] exp2 = ExportToXlsx.Export(exp);

            context.Response.ClearHeaders();
            context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            context.Response.AddHeader("Content-disposition", "attachment;filename=ikarsteinExcelExport.xlsx");
            context.Response.AddFileDependency("ikarsteinExcelExport.xlsx");
            context.Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(0, 0, 0, 1));

            context.Response.OutputStream.Write(exp2, 0, exp2.Length);
            context.Response.OutputStream.Close();

            /*TextWriter tw = new StreamWriter(context.Response.OutputStream);
            tw.WriteLine("<html><body><pre>" + HttpUtility.HtmlEncode(exp) + "</pre></body></html>");
            tw.Close();
            context.Response.OutputStream.Close();*/
        }
    }
}
